Git is a version control system. Version control helps you to track the files and show the changes you have made in your project. You can create a snapshot of your the files in your project any time. A project or repository is basically a folder where you have all your codes, files, subfolders, etc on your local computer.
You can upload this repository to a remote server (GitHub, GitLab, etc), so you won’t depend only on your computer. This way others can collaborate in your project or you can download (which is cloning in Git terms) a project made by someone else and collaborate in it.
Git is good for a number of reasons:
final_v3_this_is_really_the_final.doc. Instead, you can create a snapshot of your file as often you would like when working on it, so you will have a saved version of your file over time.There are 3+1 stages in Git: the Working Directory, the Staging Area, the local repository and the remote repository.
The Working Directory is the folder of your project. This is the directory whose files Git tracks. Git doesn’t actually track all files, it only tracks the files you tell it to track.
Git starts to track a file, when you add it to the Staging Area.
You can create a snapshot of a bunch of staged files whenever you want. This process is called commiting. When you make a commit, Git stores on your local repository how the file looks like at the time of the commit. You can also check what changes were made since your previous commit of that file. This way you will have the full history of your files and thus your project.
You probably want to store your project’s history on a remote repository like GitHub, so it won’t be all lost if something happens to your computer.
First you need to download Git. You can find help on the RStudio’s website on how to install it on your computer.
This project is on GitHub. If you want to upload changes to GitHub you have made to this project, you will need to register first on GitHub.
Altough it is possible to create a local Git based project and link it later to a remote server, it is easier to do it the other way around:
Create a GitHub repository. After your repository is created, you can clone it onto your local computer:
File > New Project > Version Control > GitClone or download and then copy the address you see there (make sure you copy the http address, not ssh).You can check your current status, which shows the changes you have made since the last snapshot (or commit in Git terms - we will get to this soon).
Go to the Git panel in RStudio
Every new file to the project or the changes you have made in a file is in the ‘unstaged’ area, which means it is not tracked by Git yet. You can add a file (or the changes in it) by ticking the box next to the file. This way the file will move to the staging area:
Once you have started to make changes, you can make a snapshot of your current status at any point. This way you can go back to this point any time later. You can commit any file which is in the staging area.
Commit button, a new window will pop up. Here you will see the changes you have made since your last commit: red shows the rows you have removed and green shows new rows (if you change eg. a few letters in a row, Git will show you that you have deleted that row and created a new one.) If you have added a brand new file, you will see it in all green.After commiting your files you can press the Push button on the upper right corner.
Since others are also able to push in the repository, next time before you do anything, you should press the Pull button.
You can clone an existing project from a remote server. The steps are the same as when creating a new project based on a newly created repository on GitHub.
File > New Project > Version Control > GitClone or download and then copy the address you see there (make sure you copy the http address, not ssh).NOTE: that by only downloading the ZIP file from the project’s GitHub webpage you do not actually get a project tracked by Git. You need to actually clone the repository to have a project followed by Git on your local computer
You can do the same things in command line. There is no difference if you do it in RStudio or in command line, it is just personal preference. You need to have a Command Line Interface (such as Terminal for Mac or Command Prompt for Windows)
git init
This will only make a local Git project. If you want to connect it with a remote server, the easiest way would be to make a project first on GitHub and then clone it.
git clone remote-repository-url
git status
git diff (this shows all changes in all files)
git diff selected_file (this shows changes you have made in a selected file)
You can select a few files to add git add write_down all_selected files_here
Or you can select all files git add .
git commit -m "write your commit message here"
git push
git pull
The .gitignore file: You can write anything into the .gitignore file, which you don’t want to track and don’t want to push it in the remote repository. We do this usually to files which contains some kind of secret (eg. password) or to huge files/images. Note that this is a hidden file, which can be seen in RStudio’s file manager, but on most file managers you need enable showing hidden files to be able to see .gitignore.
Checking your history with git log: In RStudio you can get the logs by pressing on History. This command shows the commit history of the project. You can see the commit messages which can give you an idea, what happened in the project. You can use the SHA-key to go back to a certain state in your project. The SHA-key is a mixture of numbers and letters you can see when checking your history, which serves as a unique identifier for each commit. When using RStudio, only the first 8 characters are shown.
Go back to an earlier state with git checkout SHA-key: This is not possible in RStudio, only with command line. By providing the SHA-key you can go back to any (commited) state in your project. You can go back to your current state by typing git checkout master
Some more advanced stuff (not necessary to know now, but can be very useful in the future)